package pers.zh.parking.mvp.ui.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import pers.zh.parking.R;
import pers.zh.parking.data.entity.Evaluation;
import pers.zh.parking.data.entity.ParkRecord;
import pers.zh.parking.data.entity.ParkingLot;
import pers.zh.parking.data.enums.RecordStatus;
import pers.zh.parking.utils.MyUtils;
import pers.zjc.commonlibs.util.StringUtils;
import pers.zjc.commonlibs.util.TimeUtils;

public class ReservationAdapter extends BaseQuickAdapter<ParkRecord, BaseViewHolder> {

    public ReservationAdapter(@Nullable List<ParkRecord> data) {
        super(R.layout.rv_item_reservation, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ParkRecord item) {
        ParkingLot lot = item.getLot();
        Evaluation eval = item.getEvaluation();
        helper.setText(R.id.tvLotName, lot.getName())
              .setText(R.id.tvStatus, item.getStatusStr())
              .setVisible(R.id.tvCancalTip, item.getStatusEnum() == RecordStatus.CANCEL || item.getStatusEnum() == RecordStatus.LEAVED)
              .setVisible(R.id.btLeft, item.getStatusEnum() == RecordStatus.RESERVING)
              .setVisible(R.id.btRight, item.getStatusEnum() != RecordStatus.CANCEL && item.getStatusEnum() != RecordStatus.EVALUATED)
              .setVisible(R.id.tvStatus, item.getStatusEnum() != RecordStatus.EVALUATED)
              .setVisible(R.id.rbEval, item.getStatusEnum() == RecordStatus.EVALUATED)
              .setText(R.id.btRight, item.getStatusEnum() == RecordStatus.PARKING ? "支付" : item.getStatusEnum() == RecordStatus.LEAVED ? "评价" : "导航")
              .setText(R.id.tvCancalTip, item.getStatusEnum() == RecordStatus.CANCEL ? mContext.getResources().getString(R.string.reservation_canceled) : mContext.getResources().getString(R.string.parking_done))
              .addOnClickListener(R.id.btLeft, R.id.btRight);
        if (item.getStatusEnum() == RecordStatus.EVALUATED) {
            helper.setRating(R.id.rbEval, eval.getEvalLevel());
        }
        genLotInfo(helper, item, lot, eval);
    }

    private void genLotInfo(BaseViewHolder helper, ParkRecord item, ParkingLot lot, Evaluation eval) {
        LinearLayout llRsvInfo = helper.itemView.findViewById(R.id.llRsvInfo);
        // 解决recyclerView复用导致多次调用addView,重复的问题
        llRsvInfo.removeAllViews();
        Map<String, String> map = new LinkedHashMap<>();
        map.put("车位", String.valueOf(item.getSpaceId()));
        map.put("车牌", String.valueOf(item.getCarNo()));
        map.put("位置", lot.getLocation());
        map.put("计费标准", lot.getPriceStr());
        if (item.getStatusEnum() == RecordStatus.PARKING) {
            if (item.getDuration() == 0) {
                int dur = MyUtils.calculateTimeDifferenceBySimpleDateFormat(item.getEntryTime(),
                        TimeUtils.date2String(new Date()));
                item.setDuration(dur);
                item.setFees((long)(dur * lot.getPrice()));
            }

            map.put("停车时长", String.format("%s小时", item.getDuration()));
            map.put("预计费用", String.format("%s元", item.getFees()));
        }
        if (item.getStatusEnum() == RecordStatus.LEAVED) {
            map.put("停车时长", String.format("%s小时", item.getDuration()));
            map.put("停车费用", String.format("%s元", item.getFees()));
        }
        switch (item.getStatusEnum()) {
            case EVALUATED:
                if (!StringUtils.isEmpty(eval.getEvalContent())) {
                    map.put("评价",eval.getEvalContent());
                }
            case LEAVED:
                if (!StringUtils.isEmpty(item.getLeaveTime())) {
                    map.put("离开时间",item.getLeaveTime());
                }
            case PARKING:
                if (!StringUtils.isEmpty(item.getEntryTime())) {
                    map.put("入场时间",item.getEntryTime());
                }
            case RESERVING:
            case CANCEL:
                if (!StringUtils.isEmpty(item.getReserveTime())) {
                    map.put("预约时间",item.getReserveTime());
                }
                break;
            default:
                break;
        }



        for (Map.Entry<String, String> entry : map.entrySet()) {
            View view = LayoutInflater.from(mContext)
                                      .inflate(R.layout.item_hor_key_value,
                                              (ViewGroup)helper.itemView.getParent(), false);
            TextView tvKey = view.findViewById(R.id.tvKey);
            TextView tvValue = view.findViewById(R.id.tvValue);
            tvKey.setText(entry.getKey());
            tvValue.setText(entry.getValue());
            llRsvInfo.addView(view);
        }
    }
}
